package com.atguigu.hadoop.examples.mapreduce;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.aggregate.ValueAggregatorBaseDescriptor;
import org.apache.hadoop.mapreduce.lib.aggregate.ValueAggregatorJob;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

public class AggregateWordHistogram
{
    public static void main(String[] args)
            throws IOException, InterruptedException, ClassNotFoundException
    {
        Job job = ValueAggregatorJob.createValueAggregatorJob(args, new Class[] { AggregateWordHistogramPlugin.class });

        job.setJarByClass(AggregateWordCount.class);
        int ret = job.waitForCompletion(true) ? 0 : 1;
        System.exit(ret);
    }

    public static class AggregateWordHistogramPlugin extends ValueAggregatorBaseDescriptor
    {
        public ArrayList<Map.Entry<Text, Text>> generateKeyValPairs(Object key, Object val)
        {
            String[] words = val.toString().split(" |\t");
            ArrayList retv = new ArrayList();
            for (int i = 0; i < words.length; i++) {
                Text valCount = new Text(words[i] + "\t1");
                Map.Entry en = generateEntry("ValueHistogram", "WORD_HISTOGRAM", valCount);

                retv.add(en);
            }
            return retv;
        }
    }
}